package org.xbmc.android.xbmcremote;

import java.io.IOException;
import org.xbmc.eventclient.XBMCClient;
import android.util.Log;

/**
 * Contains a XBMC Keymapping entry.
 * @author Stefan Agner
 *
 */
class XBMCEventMapping 
{
	// Mapping Types...
	private static final int XBMC_MAPPING_KEYCODE = 1;
	private static final int XBMC_MAPPING_MAPPING_NAME = 2;
	private static final int XBMC_MAPPING_MAPPING_CODE = 3;
	private static final int XBMC_MAPPING_ACTION = 4;
	
	private int button_type;
	private String button_name;
	private String map_name;
	private String action;
	private int code;
	private boolean sendupanddown;

	public XBMCEventMapping(String map_name, String button_name, boolean sendupanddown)
	{
		button_type = XBMC_MAPPING_MAPPING_NAME;
		this.button_name = button_name;
		this.map_name = map_name;
		this.sendupanddown = sendupanddown; 
	}
	
	public XBMCEventMapping(String map_name, int code, boolean sendupanddown)
	{
		button_type = XBMC_MAPPING_MAPPING_CODE;
		this.code = code;
		this.map_name = map_name;
		this.sendupanddown = sendupanddown; 
	}
	
	public XBMCEventMapping(int code, boolean sendupanddown)
	{
		button_type = XBMC_MAPPING_KEYCODE;
		this.code = code;
		this.sendupanddown = sendupanddown; 
	}
	
	public XBMCEventMapping(String action)
	{
		button_type = XBMC_MAPPING_ACTION;
		this.action = action;
	}

	public boolean sendButton(XBMCClient oXBMCClient, boolean down)
    {
		if(!sendupanddown && !down)
			return true;
    	try {
    		if(button_type == XBMC_MAPPING_MAPPING_NAME)
    		{
    			oXBMCClient.sendButton(map_name, button_name, sendupanddown, down, false, (short)0 , (byte)0);
    			Log.e("XBMCRemote", button_name + " sent");
    		}
    		else if (button_type == XBMC_MAPPING_MAPPING_CODE)
    			oXBMCClient.sendButton(map_name, (short)code, sendupanddown, down, false, (short)0 , (byte)0);
    		else if (button_type == XBMC_MAPPING_KEYCODE)
    			oXBMCClient.sendButton((short)code, sendupanddown, down, false, (short)0 , (byte)0);
    		else if (button_type == XBMC_MAPPING_ACTION)
    			oXBMCClient.sendAction(action);
    		else
    			Log.e("XBMCRemote", "Illegal Button Type");
    	} catch (IOException e) {
    		return false; // Somehow the event couldn't be handled...
    	}
    	return true;
    }
	
}